﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace SFX
{
    class SingleEffect
    {

        List<Texture2D> EffectImages;
        public bool IsFinished;
        bool PlayEffect;
        float AnimationTimer;
        int CurrentFrame;
        Vector2 EffectPosition;
        Color EffectColor;
        public SingleEffect(List<Texture2D> images, Vector2 position)
        {
            EffectImages = images;
            EffectPosition = position;
            EffectColor = Color.White;
            AnimationTimer = 0;
            CurrentFrame = 0;
            IsFinished = false;
            PlayEffect = true;
        }


        public void Update(float ElapsedTime)
        {

            if (PlayEffect)
            {
                AnimationTimer += 0.5f;
                if (AnimationTimer > 1)
                {
                    CurrentFrame++;
                    if (CurrentFrame >= EffectImages.Count)
                    {
                        IsFinished = true;
                        PlayEffect = false;
                    }
                }
            }
        }
        public void Render(float Elapsed)
        {
            if (PlayEffect)
            {
                RoyalRumble.MainGame.spriteBatch.Draw(EffectImages[CurrentFrame], EffectPosition, null, EffectColor, 0, new Vector2(EffectImages[0].Width * 0.5f, EffectImages[0].Height * 0.5f), 1.0f, SpriteEffects.None, 0);
            }
        }
    }

    public class AnimatedEffects
    {
        public enum EffectType { Explosion = 0, Smoke }
        public TextureManager textureManager;
        List<SingleEffect> CurrentEffects;

        private static AnimatedEffects animatedEffects;

        private AnimatedEffects()
        {
            textureManager = new TextureManager(RoyalRumble.MainGame.theContent);
            textureManager.AddTexture("EXPLOSION1", "Effects/explosion1");
            textureManager.AddTexture("EXPLOSION2", "Effects/explosion2");
            textureManager.AddTexture("EXPLOSION3", "Effects/explosion3");
            textureManager.AddTexture("EXPLOSION4", "Effects/explosion4");
            CurrentEffects = new List<SingleEffect>();
        }

        public static AnimatedEffects AnimatedEffectsInstance
        {
            get
            {
                if (animatedEffects == null)
                    animatedEffects = new AnimatedEffects();

                return animatedEffects;
            }
        }

        public void InitializeEffects()
        {
        }

        public void PlayExplosion(EffectType effectType, Vector2 playPosition)
        {
            if (effectType == EffectType.Explosion)
            {
                List<Texture2D> effectImages = new List<Texture2D>();
                effectImages.Add(textureManager.GetTexture("EXPLOSION1"));
                effectImages.Add(textureManager.GetTexture("EXPLOSION2"));
                effectImages.Add(textureManager.GetTexture("EXPLOSION3"));
                effectImages.Add(textureManager.GetTexture("EXPLOSION4"));

                SingleEffect newEffect = new SingleEffect(effectImages, playPosition);
                CurrentEffects.Add(newEffect);
            }
        }

        public void Update(float ElapsedTime)
        {

            if (CurrentEffects.Count > 0)
            {
                for (int i = 0; i < CurrentEffects.Count; i++)
                {
                    if (CurrentEffects[i].IsFinished)
                        CurrentEffects.RemoveAt(i);
                }
                for (int i = 0; i < CurrentEffects.Count; i++)
                    CurrentEffects[i].Update(ElapsedTime);
            }
        }

        public void Render(float ElapsedTime)
        {
            if (CurrentEffects.Count > 0)
            {
                for (int i = 0; i < CurrentEffects.Count; i++)
                {
                    if (CurrentEffects[i].IsFinished == false)
                        CurrentEffects[i].Render(ElapsedTime);
                }
            }
        }

    }
}